package cn.shop.merchant.service;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.util.StrUtil;
import cn.shop.merchant.mapper.gametemplate.GameInfoMapper;
import cn.shop.merchant.model.gameconfig.GameInfo;
import cn.shop.merchant.model.gameconfig.GameTemplate;
import cn.shop.merchant.req.GameInfoQueryReq;
import cn.shop.utils.PageUtil;
import cn.shop.utils.Putif;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : 李一涵
 * @date : Created in 2022-2-26 10:04
 * @description :
 */
@Service
public class GameInfoService extends BaseServiceImpl<GameInfoMapper, GameInfo> {

    public Map<String, Object> pageList(long uid, GameInfoQueryReq req) {
        Page<GameInfo> page = Page.of(req.getCurrent(), req.getSize());
        page = baseMapper.gameInfoList(page, uid, req);
        return PageUtil.formatPage(page, this::format);
    }

    public List<Map<String, Object>> allGame(long uid, long aliasId) {
        LambdaQueryWrapper<GameInfo> query = Wrappers.<GameInfo>lambdaQuery()
                .eq(GameInfo::getUid, uid)
                .eq(GameInfo::getAliasId, aliasId);
        List<GameInfo> list = list(query);
        return list.stream().map(gameInfo ->
            Putif.start()
                .anyway("name", gameInfo.getName())
                .get()
        ).collect(Collectors.toList());
    }

    public boolean deleteByIds(long id, long uid) {
        return SqlHelper.retBool(getBaseMapper().batchDeleteWithFill(new GameInfo(), Wrappers.<GameInfo>lambdaQuery()
                .eq(GameInfo::getId, id)
                .eq(GameInfo::getUid, uid)));
    }

    public long countByTemplateId(long templateId) {
        return count(Wrappers.<GameInfo>lambdaQuery().eq(GameInfo::getTemplateId, templateId));
    }

    public long countByAliasId(long aliasId) {
        return count(Wrappers.<GameInfo>lambdaQuery().eq(GameInfo::getAliasId, aliasId));
    }

    public List<Map<String, Object>> allList(long uid) {
        return list(Wrappers.<GameInfo>lambdaQuery()
                .eq(GameInfo::getUid, uid)).stream()
                .map(t -> Putif.start()
                        .anyway("value", t.getId())
                        .anyway("label", t.getName())
                        .get())
                .collect(Collectors.toList());
    }

    private Map<String, Object> format(GameInfo info) {
        return Putif.start()
                .anyway("id", info.getId())
                .anyway("template", info.getTemplateName())
                .anyway("alias", info.getAlias())
                .anyway("name", info.getName())
                .anyway("time", info.getCreateTime())
                .get();
    }

}
